package com.hongxinguoji.manager.admin.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hongxinguoji.manager.admin.mapper.HxAuthFunctionMapper;
import com.hongxinguoji.manager.admin.mapper.HxAuthRoleMapper;
import com.hongxinguoji.manager.admin.mapper.HxRoleFunctionMapper;
import com.hongxinguoji.manager.admin.mapper.HxUserRoleMapper;
import com.hongxinguoji.manager.admin.pojo.HxAuthFunction;
import com.hongxinguoji.manager.admin.pojo.HxAuthRole;
import com.hongxinguoji.manager.admin.pojo.HxAuthRoleExample;
import com.hongxinguoji.manager.admin.pojo.HxRoleFunctionExample;
import com.hongxinguoji.manager.admin.pojo.HxRoleFunctionExample.Criteria;
import com.hongxinguoji.manager.admin.pojo.HxRoleFunctionKey;
import com.hongxinguoji.manager.admin.pojo.HxUserRoleExample;
import com.hongxinguoji.manager.admin.service.IRoleService;

@Service
public class RoleServiceImpl implements IRoleService {

	@Autowired
	private HxAuthRoleMapper roleMapper;

	@Autowired
	private HxAuthFunctionMapper functionMapper;

	@Override
	public List<HxAuthRole> getRoleList() {

		// 查询所有数据
		HxAuthRoleExample example = new HxAuthRoleExample();
		List<HxAuthRole> list = roleMapper.selectByExample(example);
		return list;

	}

	@Autowired
	private HxRoleFunctionMapper roleFunctionMapper;

	@Override
	public void addRole(String ids, HxAuthRole role) {

		// 保存角色数据
		// 获取id
		String stringId = role.getId();

		roleMapper.insert(role);

		/*
		 * 循环添加外键
		 */
		if (!"".equals(ids)) {
			// 关联外键关系
			String[] id = ids.split(",");
			for (String i : id) {
				// 关联关系
				HxRoleFunctionKey roleFunctionKey = new HxRoleFunctionKey();
				roleFunctionKey.setFunctionId(i);
				roleFunctionKey.setRoleId(stringId);

				// 保存关系
				roleFunctionMapper.insert(roleFunctionKey);
			}

		}

	}

	@Override
	public List<HxAuthRole> getallRoles() {
		HxAuthRoleExample example = new HxAuthRoleExample();
		List<HxAuthRole> list = roleMapper.selectByExample(example);
		return list;
	}

	@Override
	public List<HxAuthFunction> getFunctionByRoleId(String roleid) {

		// 通过角色id，查询关联的权限
		HxRoleFunctionExample example = new HxRoleFunctionExample();
		Criteria criteria = example.createCriteria();
		criteria.andRoleIdEqualTo(roleid);
		// 角色权限列表
		List<HxRoleFunctionKey> list = roleFunctionMapper.selectByExample(example);

		// 通过角色查询权限
		List<HxAuthFunction> functions = new ArrayList<HxAuthFunction>();

		for (HxRoleFunctionKey hxRoleFunctionKey : list) {
			HxAuthFunction hxAuthFunction = functionMapper.selectByPrimaryKey(hxRoleFunctionKey.getFunctionId());
			functions.add(hxAuthFunction);

		}

		return functions;
	}

	@Autowired
	private HxUserRoleMapper userRoleMapper;

	@Override
	public void deleRole(String ids) {
		String[] roleid = ids.split(",");
		for (String id : roleid) {

			// 删除用户角色表中的外键
			HxUserRoleExample example = new HxUserRoleExample();
			com.hongxinguoji.manager.admin.pojo.HxUserRoleExample.Criteria criteria = example.createCriteria();
			criteria.andRoleIdEqualTo(id);
			userRoleMapper.deleteByExample(example);

			// 删除角色权限表中的外键
			HxRoleFunctionExample example1 = new HxRoleFunctionExample();
			Criteria criteria2 = example1.createCriteria();
			criteria2.andRoleIdEqualTo(id);
			roleFunctionMapper.deleteByExample(example1);

			// 删除角色表中的数据
			roleMapper.deleteByPrimaryKey(id);

		}

	}

}
